package com.mms.utsc.smartnet;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class AnnouncementsViewPager extends Activity implements OnPageChangeListener, OnClickListener {
	ArrayList<Announcement> announcements;
	ViewPager viewPager;

	TextView tvNofNAnnouncement;
	Button bPrev;
	Button bNext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.announcements_main);

		// get announcements from database
		ProgressDialog dialog = ProgressDialog.show(this, null, "Loading - Please wait...");
		DBHandler db = new DBHandler(dialog, this);
		try {
			db.updateAnnouncements();
		} catch (Exception e) {
			Toast.makeText(this, "Cannot download announcements.", Toast.LENGTH_SHORT).show();
		}

		tvNofNAnnouncement = (TextView) findViewById(R.id.tvNofNAnnouncement);
		bPrev = (Button) findViewById(R.id.bPrev);
		bNext = (Button) findViewById(R.id.bNext);
		bPrev.setOnClickListener(this);
		bNext.setOnClickListener(this);

		viewPager = (ViewPager) findViewById(R.id.vpAnnouncements);
		update();

		viewPager.setOnPageChangeListener(this);
		viewPager.setCurrentItem(0, true);
		this.onPageSelected(0);
	}

	void update() {
		ProgressDialog pd = ProgressDialog.show(this, "Updating announcements", "Loading - Please wait...");
		DBHandler db = new DBHandler(pd, this);
		LocalDBHandler localdb = db.getLocalDBHandler();
		announcements = localdb.getAllAnnouncements(null);
		db.close();
		viewPager.setAdapter(new AnnPagerAdapter());
		pd.dismiss();
	}

	class AnnPagerAdapter extends PagerAdapter {

		public AnnPagerAdapter() {

		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = (LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			View currView = inflater.inflate(R.layout.announcements_page, null);
			TextView tvContent = (TextView) currView.findViewById(R.id.tvAnnContent);
			TextView tvTitle = (TextView) currView.findViewById(R.id.tvAnnTitle);
			Announcement currAnn = announcements.get(position);

			tvTitle.setText(currAnn.getTitleText());
			tvContent.setText(currAnn.getContent());

			((ViewPager) container).addView(currView, 0);

			return currView;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return announcements.size();
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);

		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == ((View) arg1);
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		tvNofNAnnouncement.setText((arg0 + 1) + " of " + announcements.size());
		bNext.setEnabled(true);
		bPrev.setEnabled(true);
		if (viewPager.getCurrentItem() == viewPager.getAdapter().getCount() - 1) {
			bNext.setEnabled(false);
		} else if (viewPager.getCurrentItem() == 0) {
			bPrev.setEnabled(false);
		}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.bNext:
			viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
			break;
		case R.id.bPrev:
			viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
			break;
		}
	}

}
